

foreach xxx in  30_60 60_180 180_300 300_600 600_900 ///
				30_15 60_30 180_60 300_180 600_300 {
	clear
	use "cleandata\winprob_matched_dataset`xxx'_100bins"
	
	
	keep if winprob!=. & timetaken!=. & timecontrol_iv!=. & size_match==2
	
	bys matchvar: egen start_clock1=total(start_clock*(timecontrol_iv==0))
	
	xtile start_clockbin=-start_clock1, n(5)

	
	
	bys matchvar: egen timetaken_1=total(timetaken*timecontrol_iv)
	bys matchvar: egen timetaken_0=total(timetaken*!timecontrol_iv)
	gen firststage=timetaken_1-timetaken_0 if size_match==2
		
	gen marginalvalue_firststage=marginalvalueoftime*firststage
	
	
	tempfile results
	forvalues t=1/5{		
		ivreghdfe winprob (timetaken=timecontrol_iv) if `t'==start_clockbin, absorb(matchvar)
		
		summ marginalvalue_firststage if start_clockbin==`t'
		local meanmarginalvalue=r(mean)
		summ firststage if marginalvalueoftime!=. & start_clockbin==`t'
		local meanfirststage=r(mean)
		
		summ start_clock1 if start_clockbin==`t'
		local meanstart_clock=r(mean)
		
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen estimate_type="Marginal Benefit"
			gen start_clockbin=`t'
			gen meanstart_clock=`meanstart_clock'
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			cap append using `results'
			save `results', replace
		restore
		
		
		replace marginalvalue_firststage=0 if timecontrol_iv==0
	
		ivreghdfe marginalvalue_firststage (timetaken= timecontrol_iv) if `t'==start_clockbin, absorb(matchvar) cluster(matchvar)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanstart_clock=`meanstart_clock'
			gen estimate_type="Marginal Cost"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			cap append using `results'
			save `results', replace
		restore
		
	}
	preserve
	use `results', clear
	summ 
	
	if "`xxx'"=="30_60" | "`xxx'"=="30_15"{
		local mytitle="30 seconds"
	}
	if "`xxx'"=="60_180" | "`xxx'"=="60_30"{
		local mytitle="1 minute"
	}
	if "`xxx'"=="180_300" | "`xxx'"=="180_60"{
		local mytitle="3 minutes"
	}
	if "`xxx'"=="300_180" | "`xxx'"=="300_600"{
		local mytitle="5 minutes"
	}
	if "`xxx'"=="600_300" | "`xxx'"=="600_900"{
		local mytitle="10 minutes"
	}
	
	twoway rspike min95 max95 meanstart_clock if estimate_type=="Marginal Benefit", color(forest_green) || ///
		scatter estimate meanstart_clock if estimate_type=="Marginal Benefit", color(forest_green ) c(l) || /// 
		rspike min95 max95 meanstart_clock if estimate_type=="Marginal Cost", color(dkorange) || ///
		scatter estimate meanstart_clock if estimate_type=="Marginal Cost", color(dkorange ) c(l) lpattern(dash) ///
		name(start_clocks_`xxx', replace) nodraw title("`mytitle'") legend(order(2 4) label(2 "Marginal Benefit") label(4 "Marginal Cost")) ///
		xtitle("Clock (seconds)") yscale(range(0)) ylabel(#3) xlabel(#3) yline(0, lcolor(gs12))
	
	restore
	
}
//
//
// graph combine start_clocks_30_15 start_clocks_60_30 start_clocks_180_60 start_clocks_300_180 start_clocks_600_300 start_clocks_900_600, name(lower, replace)
// graph export "start_clock_comparison_lower.pdf", as(pdf) replace
//
// graph combine start_clocks_15_30 start_clocks_30_60 start_clocks_60_180 start_clocks_180_300 start_clocks_300_600 start_clocks_600_900, name(higher, replace)
// graph export "start_clock_comparison_higher.pdf", as(pdf) replace



grc1leg  start_clocks_30_60 start_clocks_60_180 start_clocks_180_300 start_clocks_300_600 start_clocks_600_900 start_clocks_30_15 start_clocks_60_30 start_clocks_180_60 start_clocks_300_180 start_clocks_600_300, row(2) 
graph export "exhibits/start_clock_comparison_100bins.pdf", as(pdf) replace
